package com.greate.community.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.greate.community.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据 id 查询用户
     * @param id
     * @return
     */
    User selectById (int id);

    /**
     * 根据 account 查询用户
     * @param account
     * @return
     */
    User selectByAccount(String account);

    /**
     * 根据 email 查询用户
     * @param email
     * @return
     */
    User selectByEmail(String email);

    /**
     * 插入用户（注册）
     * @param user
     * @return
     */
    int insertUser(User user);

    /**
     * 修改用户状态
     * @param id
     * @param status 0：未激活，1：已激活
     * @return
     */
    int updateStatus(int id, int status);

    /**
     * 修改头像
     * @param id
     * @param headerUrl
     * @return
     */
    int updateHeader(int id, String headerUrl);

    /**
     * 修改密码
     * @param id
     * @param password 新密码
     * @return
     */
    int updatePassword(int id, String password);

    @Select("SELECT * FROM user")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "account", property = "account"),
            @Result(column = "password", property = "password"),
            @Result(column = "salt", property = "salt"),
            @Result(column = "email", property = "email"),
            @Result(column = "status", property = "status"),
            @Result(column = "type", property = "type"),
            @Result(column = "activationCode", property = "activationCode"),
            @Result(column = "header_url", property = "headerUrl"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "school_id", property = "school",
                    one = @One(select = "com.greate.community.dao.SchoolMapper.selectById"))
    })
    List<User> selectUsersWithSchool();

}
